package com.pap.msg.feedback.controller;

import com.pap.base.util.bean.BeanCopyUtilss;
import com.pap.base.util.string.StringUtilss;
import com.pap.beans.idworker.IdWorker;
import com.pap.msg.dto.FeedbackDTO;
import com.pap.msg.feedback.entity.Feedback;
import com.pap.msg.feedback.service.IFeedbackService;
import com.pap.obj.vo.loginedapiuser.LoginedAPIUserVO;
import com.pap.obj.vo.logineduser.LoginedUser;
import com.pap.obj.vo.logineduser.LoginedUserVO;
import com.pap.obj.vo.page.Page;
import com.pap.obj.vo.response.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/feedback")
@Api(value = "意见反馈", tags = "意见反馈", description="意见反馈")
public class FeedbackController {

	private static Logger logger  =  LoggerFactory.getLogger(FeedbackController.class);
	
	@Resource(name = "feedbackService")
	private IFeedbackService feedbackService;

	@Resource(name = "idWorker")
	private IdWorker idWorker;

	@ApiOperation("创建")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "feedbackDTO", value = "", required = true, dataType = "FeedbackDTO", paramType="body")
	})
	@PostMapping(value = "")
	public ResponseVO<FeedbackDTO> create(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
										  @RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
										  @RequestBody FeedbackDTO feedbackDTO,
										  @LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){
		String idStr = idWorker.nextIdStr();
		// TODO 设置主键
		feedbackDTO.setFeedbackId(idStr);

		Feedback databaseObj = new Feedback();
		BeanCopyUtilss.myCopyProperties(feedbackDTO, databaseObj, loginedUserVO);

		int i = feedbackService.insertSelective(databaseObj);

		return ResponseVO.successdata(feedbackDTO);
	}

	@ApiOperation("更新")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "feedbackDTO", value = "", required = true, dataType = "FeedbackDTO", paramType="body")
	})
	@PutMapping(value = "")
	public ResponseVO<FeedbackDTO> update(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@RequestBody FeedbackDTO feedbackDTO,
													@LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){

		Feedback databaseObj = new Feedback();
		BeanCopyUtilss.myCopyProperties(feedbackDTO, databaseObj, loginedUserVO, false);
		int i = feedbackService.updateByPrimaryKeySelective(databaseObj);

		// TODO 主键部分
		databaseObj = feedbackService.selectByPrimaryKey(databaseObj.getFeedbackId());

		return ResponseVO.successdata(databaseObj);
	}


	@ApiOperation("删除")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "feedbackDTO", value = "", required = true, dataType = "FeedbackDTO", paramType="body")
	})
	@DeleteMapping(value = "")
	public ResponseVO<String> delete(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@RequestBody FeedbackDTO feedbackDTO,
													@LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){

		Feedback databaseObj = new Feedback();
		BeanCopyUtilss.myCopyProperties(feedbackDTO, databaseObj);

		// TODO 主键部分
		int i = feedbackService.deleteByPrimaryKey(databaseObj.getFeedbackId());

		return ResponseVO.successdata("操作成功");
	}

	@ApiOperation("详情")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "id", value = "编号", required = true, dataType = "String", paramType="path")
	})
	@GetMapping(value = "/{id}")
	public ResponseVO<FeedbackDTO> selectOne(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@PathVariable("id") String id){

		Feedback databaseObj = feedbackService.selectByPrimaryKey(id);

		return ResponseVO.successdata(databaseObj);
	}

	@ApiOperation("查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "feedbackDTO", value = "应用查询参数", required = false, dataType = "FeedbackDTO", paramType="body"),
		@ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int", paramType="query"),
		@ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType = "int", paramType="query"),
		@ApiImplicitParam(name = "sortBy", value = "按照哪个属性排序", required = false, dataType = "String", paramType="query"),
		@ApiImplicitParam(name = "order", value = "排序顺序", required = false, dataType = "String", paramType="query")
	})
	@PostMapping(value = "/query")
	public ResponseVO<FeedbackDTO>query(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
												@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
												@RequestBody FeedbackDTO feedbackDTO,
												@RequestParam Integer pageNo, @RequestParam Integer pageSize,
												@RequestParam(defaultValue = "create_time") String sortBy, @RequestParam(defaultValue = "desc") String order,
												@LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){
		Map<Object, Object> map = BeanCopyUtilss.beanToMap(feedbackDTO, loginedUserVO);
		if(pageSize != 0) {
			Page newPage = new Page(pageNo + "", pageSize + "");
			map.put("page", newPage);
		}
		List<Feedback> list = feedbackService.selectListByMap(map);

		//
		List<FeedbackDTO> dtoList = toDTO(list);

		if(pageSize != 0) {
			Page newPage = new Page(pageNo + "", pageSize + "");
			int countNum = feedbackService.selectCountByMap(map);
			newPage.setCount(countNum);
			return ResponseVO.successdatas(dtoList, newPage);
		} else {
			return ResponseVO.successdatas(dtoList, null);
		}

	}

	@ApiOperation("提交意见反馈")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "feedbackDTO", value = "", required = true, dataType = "FeedbackDTO", paramType = "body")
	})
	@PostMapping(value = "/api/submitfeedback")
	public ResponseVO<FeedbackDTO> submitAuditInfo(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													   @RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													   @RequestBody FeedbackDTO feedbackDTO,
													   @ApiIgnore LoginedAPIUserVO loginedAPIUserVO) {
		if(loginedAPIUserVO != null && StringUtilss.isNotEmpty(loginedAPIUserVO.getId())) {
			Feedback feedback = new Feedback();
			feedback.setFeedbackId(idWorker.nextIdStr());
			feedback.setFeedbackCode("");
			feedback.setFeedbackUserId(loginedAPIUserVO.getId());
			feedback.setFeedbackUserName(loginedAPIUserVO.getName());
			feedback.setFeedbackUserMobile(feedbackDTO.getFeedbackUserMobile());
			feedback.setFeedbackContent(feedbackDTO.getFeedbackContent());

			int operationInt = feedbackService.insertSelective(feedback);
			return ResponseVO.successdata(feedback);
		} else {
			return ResponseVO.exceptiondata("登录信息异常，请登录后重新操作!");
		}

	}

	private List<FeedbackDTO> toDTO(List<Feedback> databaseList) {
		List<FeedbackDTO> returnList = new ArrayList<FeedbackDTO>();
		if(databaseList != null) {
			for(Feedback dbEntity : databaseList) {
				FeedbackDTO dtoTemp = new FeedbackDTO();
				BeanCopyUtilss.myCopyProperties(dbEntity, dtoTemp);
				returnList.add(dtoTemp);
			}
		}
		return returnList;
	}

	private List<Feedback> toDB(List<FeedbackDTO> dtoList) {
		List<Feedback> returnList = new ArrayList<Feedback>();
		if(dtoList != null) {
			for(FeedbackDTO dtoEntity : dtoList) {
				Feedback dbTemp = new Feedback();
				BeanCopyUtilss.myCopyProperties(dtoEntity, dbTemp);
				returnList.add(dbTemp);
			}
		}
		return returnList;
	}
}
